<template>
  <div>
    <el-row>
      <el-col :span="8" :offset="8">
        <div>
          <p class="title">小U商城后台管理系统</p>
          <!-- model:表单的数据 -->
          <!-- rules:校验的规则 -->
          <!-- ref:查找dom节点 -->
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="用户名" prop="username">
              <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="ruleForm.password"></el-input>
            </el-form-item>
             <el-form-item>
              <el-button type="success" @click="submitForm('ruleForm')">立即登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import {adminLogin} from '../../request'
// 引入辅助函数，其中mapMutations， mapActions要映射到methods中
// mapState， mapGetters要映射到computed中
import {mapMutations} from 'vuex'
export default {
  data() {
    return {
      // 表单的数据
      ruleForm: {
        username: 'admin',
        password: 'admin888',
      },
      // 校验规则
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    // 映射mapMutations
    ...mapMutations({
      saveUserInfo: 'saveUserInfo'
    }),
    // 点击按钮去登陆，先校验，成功只后才发请求
    submitForm() {
      // $refs.ruleForm找到表单，校验
      this.$refs.ruleForm.validate( async (valid) => {
        if (valid) {
          // 校验成功
          let res = await adminLogin(this.ruleForm);
          if(res.list) {
            // this.$store.commit('saveUserInfo', res.list);
            this.saveUserInfo(res.list)
          }
          // this.$router.push('/index')
        } else {
          // 检验失败
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
};
</script>
<style lang="">
</style>